{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Y2eqHW0TK87s"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/drive/155vRTkn8rB12iSgaoay8pUJxvWzFtCoo?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "\n",
        "### 🚀 Zero-Shot Chain of Thought (Zero-Shot-CoT)\n",
        "\n",
        "Zero-Shot-CoT is a variation of Chain of Thought prompting that doesn't rely on examples.\n",
        "\n",
        "### Key points:\n",
        "\n",
        "* 🔑 **Core concept:** Encourages step-by-step reasoning without providing sample solutions.\n",
        "\n",
        "* 📝 **Prompt structure:** Typically includes phrases like \"Let's approach this step-by-step\" or \"Let's think about this logically.\"\n",
        "\n",
        "* 🌟 **Advantage:** Flexibility across various tasks without task-specific examples.\n",
        "\n",
        "* ⚖️ **Challenge:** Relies heavily on the model's inherent reasoning capabilities.\n",
        "\n",
        "* 💼 **Applications:** Problem-solving, analysis, and decision-making across diverse domains.\n",
        "\n",
        "* 🚀 **Implementation:** Often uses a two-stage process - reasoning generation followed by answer extraction.\n",
        "\n",
        "* 📝 **Example prompt:** \"Let's solve this problem step-by-step: [insert problem]\"\n",
        "\n",
        "* 📈 **Effectiveness:** Can significantly improve performance on complex tasks compared to direct questioning."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "XNrPX8MZJm9e",
        "outputId": "24857b5b-726f-4f31-eadf-2990ca59286d"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/121.9 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━\u001b[0m \u001b[32m112.6/121.9 kB\u001b[0m \u001b[31m4.6 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m121.9/121.9 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ],
      "source": [
        "!pip install -q -U langchain-groq>=0.2.4"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "gOLahJCEJv6_"
      },
      "outputs": [],
      "source": [
        "from langchain_groq import ChatGroq\n",
        "from langchain.prompts import ChatPromptTemplate"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "I6qvHWQsJxWq"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "import getpass"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GaoPiWagJyoV"
      },
      "source": [
        "### 🔑 Provide Groq API Key\n",
        "\n",
        "- [Groq API Key](https://console.groq.com/keys)\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f-Ioab8HJ037",
        "outputId": "b190708b-07de-47d5-e492-6f276bff3fc2"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "··········\n"
          ]
        }
      ],
      "source": [
        "os.environ[\"GROQ_API_KEY\"] = getpass.getpass()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "RzCcry2GJ2Z1"
      },
      "outputs": [],
      "source": [
        "llm = ChatGroq(\n",
        "    model=\"llama-3.1-8b-instant\",\n",
        "    temperature=0.5\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "RCjbg-xkJ4cY"
      },
      "outputs": [],
      "source": [
        "zero_shot_cot_prompt = ChatPromptTemplate.from_messages(\n",
        "    [\n",
        "        (\"system\", \"Approach problems step-by-step, explaining your reasoning at each stage.\"),\n",
        "        (\"human\", \"Q: {input}\\nA: Let's approach this step-by-step:\"),\n",
        "    ]\n",
        ")\n",
        "\n",
        "chain = zero_shot_cot_prompt | llm"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "BliFnvhIJ-66",
        "outputId": "eceb6d5c-7094-421f-f1e2-5ca9301c5938"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Let's break this problem down step by step.\n",
            "\n",
            "Step 1: Identify the given information.\n",
            "The store has 100 apples, and 20% of the apples are rotten.\n",
            "\n",
            "Step 2: Determine what 20% of 100 apples is.\n",
            "To find 20% of 100, we can multiply 100 by 0.20 (which is the same as 20/100).\n",
            "100 x 0.20 = 20\n",
            "\n",
            "So, 20 apples are rotten.\n",
            "\n",
            "Step 3: Find the number of good apples.\n",
            "To find the number of good apples, we subtract the number of rotten apples from the total number of apples.\n",
            "100 - 20 = 80\n",
            "\n",
            "Therefore, there are 80 good apples left.\n"
          ]
        }
      ],
      "source": [
        "question =  \"A store has 100 apples. If 20% of the apples are rotten, how many good apples are left?\"\n",
        "response = chain.invoke({\"input\": question})\n",
        "print(response.content)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "YCnIpHUSKWsT"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
